<template>
  <div id="app">
    <nav class="main-nav">
      <router-link to="/">首页</router-link> |
      <router-link to="/public-activities">公开活动列表</router-link> |
      <router-link v-if="isAuthenticated && userRole === 'admin'" to="/activity-management">活动管理</router-link> |
      <a v-if="isAuthenticated" href="#" @click.prevent="logout">退出登录</a>
      <router-link v-else to="/login">登录</router-link>
    </nav>
    <router-view class="main-content"/>
  </div>
</template>

<script>
export default {
  computed: {
    isAuthenticated() {
      return !!localStorage.getItem('userRole');
    },
    userRole() {
      return localStorage.getItem('userRole');
    },
  },
  methods: {
    logout() {
      localStorage.removeItem('userRole');
      if (this.$route.path !== '/') {
        this.$router.push('/');
      }
    },
  },
  watch: {
    $route(to, from) {
      // 当路由变化时，重新加载页面
      window.location.reload();
    }
  }
};
</script>

<style>
body {
  margin: 0;
  background-color: #f5f5f5;
  font-family: 'Arial', sans-serif;
}

#app {
  max-width: 1200px;
  margin: 0 auto;
}

.main-nav {
  background: #2c3e50;
  padding: 1rem;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.main-nav a {
  color: #ecf0f1;
  text-decoration: none;
  margin: 0 1rem;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.main-nav a:hover {
  background: #34495e;
}

.main-nav a.router-link-exact-active {
  color: #42b983;
  font-weight: bold;
}

.main-content {
  padding: 2rem;
  background: white;
  min-height: calc(100vh - 120px);
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
</style>